package com.musicmall.service.Impl;

import com.musicmall.common.Const;
import com.musicmall.common.ServerResponse;
import com.musicmall.dao.UserMapper;
import com.musicmall.pojo.User;
import com.musicmall.service.IUserService;
import com.musicmall.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.JobOriginatingUserName;

@Service("iUserService")
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public ServerResponse login(String username, String password){
        int count = userMapper.checkUserName(username);
        if(count==0){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }

        String md5Password = MD5Util.MD5EncodeUtf8(password);
        User user = userMapper.selectLogin(username,md5Password);
        if(user==null){
            return ServerResponse.createByErrorMessage("用户密码错误");
        }
        user.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登录成功",user);
    }

    public ServerResponse register(User user){
        //1 校验用户名是否存在
        //2 校验email是否存在
        ServerResponse validResponse = this.checkvalid(user.getUsername(), Const.USERNAME);
        if(!validResponse.isSuccess()){
            return validResponse;
        }
        validResponse = this.checkvalid(user.getEmail(),Const.EMAIL);
        if(!validResponse.isSuccess()){
            return validResponse;
        }
        validResponse = this.checkvalid(user.getPhone(), Const.PHONE);
        if(!validResponse.isSuccess()){
            return validResponse;
        }
//        user.setRole(Const.Role.ROLE_CUSTOMER);
//        user.setPassword(user.getPassword());
        user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
        int resultCount = userMapper.insert(user);
        if(resultCount==0){
            return ServerResponse.createByErrorMessage("注册失败");
        }
        return ServerResponse.createBySuccess("注册成功");

    }

    public ServerResponse checkvalid(String str,String type){
        if(StringUtils.isNotBlank(type)){
            if(type.equals(Const.CURRENT_USER)){
                int resultCount = userMapper.checkUserName(str);
                if(resultCount>0){
                    return ServerResponse.createByErrorMessage("用户名已存在");
                }
            }
            if(type.equals(Const.EMAIL)){
                int resultCount =userMapper.checkEmail(str);
                if(resultCount>0){
                    return ServerResponse.createByErrorMessage("邮箱已被注册");
                }
            }
            if(type.equals(Const.PHONE)){
                int resultCount =userMapper.checkPhone(str);
                if(resultCount>0){
                    return ServerResponse.createByErrorMessage("电话已被注册");
                }
            }
        }
        else{
            return ServerResponse.createByErrorMessage("传入参数有误");
        }
        return ServerResponse.createBySuccess("校验成功");

    }

    public ServerResponse getInformation(Integer userId){
        User user = userMapper.selectByPrimaryKey(userId);
        if(user==null){
            return ServerResponse.createByErrorMessage("找不到当前用户");
        }
        user.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess(user);

    }


}
